<?php
/**
 * @name 礼物配置控制器
 */
namespace app\admin\controller;
use app\common\controller\AdminBase;
class Gift extends AdminBase {
    /**
     * @name 初始化
     */
    protected function _initialize(){
        //添加初始化方法
        $this->mod = model('admin/Config');
        parent::_initialize();
    }
    /**
     * @name 礼物配置
     */
    public function gift(){
        $this->pageTitle = '礼物管理';
        $list = $this->mod->getConfigList('gift','',8);
        $this->assign('list',$list);
        return $this->fetch('gift_list');
    }
    /**
     * @name 添加礼物
     */
    public function gift_add(){
        $this->pageTitle = '添加礼物';
        return $this->fetch('gift_add');
    }
    /**
     * @name 随机生成礼物编号
     */
    public function rand_gift_code(){
        return ['status' => 1,'data'=>['gift_code'=>$this->create_gift_code(4,true)]];
    }
    /**
     * @name 随机生成礼物编号
     */
    protected function create_gift_code($len = 6,$onlyNum = false){
        $giftCode = 'g_'.$this->mod->createCode($len,$onlyNum);
        $res = $this->mod->hasKey($giftCode,'gift');
        if($res){
            return $this->create_gift_code();
        }
        return $giftCode;
    }
    /**
     * @name 添加礼物配置
     */
    public function do_add_gift(){
        if(IS_AJAX){
            if(true !== $this->checkData(['gift_name','gift_code'])){
                return ['status'=>0,'message'=>$this->error_info];
            }
            $this->gift_gold = (int)$this->gift_gold;
            $value = [
                'name' => msubstr($this->gift_name,0,4),
                'gold' => $this->gift_gold,
                'gift_code' => $this->gift_code,
                'image' => decrypt_id($this->gift_img)
            ];
            if(!$value['image']){
                return ['status' => 0,'message'=>'请上传礼物展示图片'];
            }
            $data = [
                'type' => 'gift',
                'key'   => $value['gift_code'],
                'value' => $value,
                
            ];
            $res = $this->mod->addConfig($data,2);
            if($res){
                return ['status' => 1,'data'=> ['info'=>'添加成功','jumpurl'=>U('admin/Gift/gift')]];
            }
        }
        return ['status' => 0,'message'=>'添加失败,请重试'];
    }
    /**
     * @name 删除礼物图片
     */
    public function rm_gift_image(){
        if(IS_AJAX){
            if(true !== $this->checkData(['attach_id'])){
                return ['status'=>0,'message'=>$this->error_info];
            }
            model('attach/Attach')->deleteAttachById($this->attach_id);
            exit;
        }
    }
    /**
     * @name 编辑
     */
    public function gift_edit(){
        if(true !== $this->checkData(['config_id'])){
            return '';
        }
        $this->config_id = decrypt_id($this->config_id);
        $info = $this->mod->getConfigById($this->config_id);
        if($info){
            $this->pageTitle = '编辑礼物';
            $this->assign('config',$info);
            return $this->fetch();
        }
    }
    /**
     * @name 处理编辑
     */
    public function do_edit_gift(){
        if(IS_AJAX){
            if(true !== $this->checkData(['config_id','gift_name'])){
                return ['status'=>0,'message'=>$this->error_info];
            }
            $this->config_id = decrypt_id($this->config_id);
            $this->gift_gold = (int)$this->gift_gold;
            $value = [
                'name' => $this->gift_name,
                'gold' => $this->gift_gold,
                'image' => decrypt_id($this->gift_img),
                'gift_code' => $this->mod->getConfigKey($this->config_id)
            ];
            if(!$value['image']){
                return ['status' => 0,'message'=>'请上传礼物展示图片'];
            }
            $data = [
                'type' => 'gift',
                'key'   => $value['gift_code'],
                'value' => $value,
                'config_id' => $this->config_id
                
            ];
            $res = $this->mod->editConfig($data,2);
            if($res){
                return ['status' => 1,'data'=> ['info'=>'编辑成功','jumpurl'=>U('admin/Gift/gift')]];
            }
        }
        return ['status' => 0,'message'=>'编辑失败,未做任何修改'];
    }
}